package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.config.UserContext;
import com.zzx.entity.TUserGroup;
import com.zzx.mapper.TUserGroupMapper;
import com.zzx.service.TUserGroupService;
import com.zzx.utils.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author CD
 * @description 针对表【t_employee_group】的数据库操作Service实现
 * @createDate 2024-12-03 10:58:42
 */
@Service
public class TUserGroupServiceImpl extends ServiceImpl<TUserGroupMapper, TUserGroup>
        implements TUserGroupService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TUserGroup param) {
        LambdaQueryWrapper<TUserGroup> wrapper = new LambdaQueryWrapper<>(TUserGroup.class);
        wrapper.eq(TUserGroup::getUserGroupName, param.getUserGroupName());
        if (ObjectUtils.isEmpty(param.getUserGroupId())) {
            param.setUserGroupId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TUserGroup::getUserGroupId, param.getUserGroupId());
            EntityUtils.mergeNonNullProperties(this.getBaseMapper().selectById(param.getUserGroupId()), param);
        }
        List<TUserGroup> tUserGroupList = this.getBaseMapper().selectList(wrapper);
        if (!tUserGroupList.isEmpty()) {
            throw new RuntimeException("名称重复");
        }
        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TUserGroup param) {
        this.removeById(param.getUserGroupId());
    }

    @Override
    public List<TUserGroup> selectBy(TUserGroup param) {
        LambdaQueryWrapper<TUserGroup> wrapper = new LambdaQueryWrapper<>(TUserGroup.class);
        if (!ObjectUtils.isEmpty(param.getUserGroupName())) {
            wrapper.like(TUserGroup::getUserGroupName, param.getUserGroupName());
        }
        if (!ObjectUtils.isEmpty(param.getUserGroupCode())) {
            wrapper.like(TUserGroup::getUserGroupCode, param.getUserGroupCode());
        }
        if (!ObjectUtils.isEmpty(param.getUserGroupId())) {
            wrapper.eq(TUserGroup::getUserGroupId, param.getUserGroupId());
        }
        wrapper.eq(TUserGroup::getCompanyId, UserContext.getCompanyId());
        return this.getBaseMapper().selectList(wrapper);
    }
}




